package com.aiti.lulian.controller.newCard;

import cn.hutool.core.exceptions.ExceptionUtil;
import com.aiti.base.core.exception.BusinessException;
import com.aiti.base.core.utils.message.Message;
import com.aiti.lulian.dto.newCard.EnterpriseCardAddParamDto;
import com.aiti.lulian.entity.newCard.CardEnterpriseUserCurrentUse;
import com.aiti.lulian.service.newCard.CardEnterpriseUserCurrentUseService;
import com.aiti.lulian.service.newCard.EnterpriseNewCardService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * description:
 * auth fcb
 * date 2025-08-18 15:07
 **/
@RestController
@RequestMapping("/new-card")
@Slf4j
@Api(tags = "企业用户新名片接口")
public class EnterpriseNewCardController {

    @Autowired
    private EnterpriseNewCardService enterpriseNewCardService;
    @Autowired
    private CardEnterpriseUserCurrentUseService cardEnterpriseUserCurrentUseService;

    @GetMapping("/query-enterprise-try-user")
    @ApiOperation("查询用户是否已试用过企业版")
    @Deprecated
    public Message queryEnterpriseTryUser(@RequestParam("userId") String userId) {
        try {
            return Message.success(enterpriseNewCardService.queryTryUserCount(userId));
        }catch (Exception e) {
            if(e instanceof BusinessException) {
                return Message.fail(e.getMessage());
            }
            log.error("查询用户是否已试用异常：{}", ExceptionUtil.stacktraceToString(e));
            return Message.fail("操作失败");
        }
    }

    @PostMapping("/add-enterprise-card")
    @ApiOperation("添加企业用户新名片")
    public Message addEnterpriseCard(@RequestBody EnterpriseCardAddParamDto dto) {
        try {
            return enterpriseNewCardService.addEnterpriseCard(dto);
        }catch (Exception e) {
            if(e instanceof BusinessException) {
                return Message.fail(e.getMessage());
            }
            log.error("添加企业用户新名片异常：{}", ExceptionUtil.stacktraceToString(e));
            return Message.fail("操作失败");
        }
    }
    /**
     * 添加企业用户新名片时进行校验判断是否又相同企业的名片
     * @param dto
     * @return
     */
    @GetMapping("/existSameEnterpriseCard")
    public Message existSameEnterpriseCard(EnterpriseCardAddParamDto dto){
        return enterpriseNewCardService.existSameEnterpriseCard(dto);
    }

    @GetMapping("/query-enterprise-card-status")
    @ApiOperation("查询用户新名片状态")
    public Message queryEnterpriseCardStatus(@RequestParam("cardId") String cardId) {
        try {
            return Message.success(enterpriseNewCardService.getEnterpriseCardStatus(cardId));
        }catch (Exception e) {
            if(e instanceof BusinessException) {
                return Message.fail(e.getMessage());
            }
            log.error("查询用户新名片状态异常：{}", ExceptionUtil.stacktraceToString(e));
            return Message.fail("查询名片状态异常");
        }
    }

    @GetMapping("/query-user-current-use-enterprise-card")
    @ApiOperation("查询用户当前使用中的企业用户新名片")
    public Message queryUserCurrentUseEnterpriseCard(   @RequestParam("enterpriseId") String enterpriseId,
                                                        @RequestParam("userId") String userId) {
        try {
            return Message.success(cardEnterpriseUserCurrentUseService.queryCardEnterpriseUserCurrentUse(enterpriseId, userId));
        }catch (Exception e) {
            if(e instanceof BusinessException) {
                return Message.fail(e.getMessage());
            }
            log.error("查询用户当前使用中的企业用户新名片异常：{}", ExceptionUtil.stacktraceToString(e));
            return Message.fail("查询用户当前使用中的企业用户新名片异常");
        }
    }

    @PostMapping("/save-or-update-user-current-use-enterprise-card")
    @ApiOperation("添加用户当前使用中的企业用户新名片")
    public Message addUserCurrentUseEnterpriseCard(@RequestBody CardEnterpriseUserCurrentUse param) {
        try {
            cardEnterpriseUserCurrentUseService.saveOrUpdateCardEnterpriseUserCurrentUse(param);
            return Message.success();
        }catch (Exception e) {
            log.error("添加用户当前使用中的企业用户新名片异常：{}", ExceptionUtil.stacktraceToString(e));
            return Message.fail("操作异常");
        }
    }

}
